﻿using System;
using System.Windows.Forms;
using System.Collections;
using StockTrader.DAL.HoseAdapter.FileParser.Structure;
using StockTrader.DAL.HoseAdapter.FileParser;
using System.Drawing;
using StockTrader.DAL.HoseAdapter.DataAccess;

namespace StockTrader.DAL.HoseAdapter.BusinessLogic
{
    public class AsyncDataProcessor
    {
        DataAccessClient client = new DataAccessClient();
        private InsertRecord _invokeMe;
        private Label _lblState;
        private Type _objType;

        private delegate int InsertRecord();

        public AsyncDataProcessor(Label lblState, Type objType, int marketstat)
        {
            _lblState = lblState;
            _objType = objType;
            switch (objType.Name)
            {
                #region CTCI
                case "OrderCancellation":
                    _invokeMe = new InsertRecord(DataAccessClient.UpdateOrderCancellation);
                    break;
                case "OrderChange":
                    _invokeMe = new InsertRecord(DataAccessClient.UpdateOrderChange);
                    break;
                case "Advertisment":
                    _invokeMe = new InsertRecord(DataAccessClient.UpdateAdvertisment);
                    break;
                case "OFPTDeal":
                    _invokeMe = new InsertRecord(DataAccessClient.UpdateOFPTDeal);
                    break;
                case "TFPTDeal":
                    _invokeMe = new InsertRecord(DataAccessClient.UpdateTFPTDeal);
                    break;
                case "NewConditionedOrder":
                    _invokeMe = new InsertRecord(DataAccessClient.UpdateNewConditionedOrder);
                    break;
                case "OrderConfirmation":
                    _invokeMe = new InsertRecord(DataAccessClient.UpdateOrderConfirmation);
                    break;
                case "ConfirmOrderCancel":
                    _invokeMe = new InsertRecord(DataAccessClient.UpdateConfirmOrderCancel);
                    break;
                case "ConfirmOrderChange":
                    _invokeMe = new InsertRecord(DataAccessClient.UpdateConfirmOrderChange);
                    break;
                case "DealConfirmation":
                    _invokeMe = new InsertRecord(DataAccessClient.UpdateDealConfirmation);
                    break;
                case "PutThroughACK":
                    _invokeMe = new InsertRecord(DataAccessClient.UpdatePutThroughACK);
                    break;
                case "Reject":
                    _invokeMe = new InsertRecord(DataAccessClient.UpdateReject);
                    break;
                case "CrossingDealConfirm":
                    _invokeMe = new InsertRecord(DataAccessClient.UpdateCrossingDealConfirm);
                    break;
                case "PutThroughDealConfirmation":
                    _invokeMe = new InsertRecord(DataAccessClient.UpdatePutThroughDealConfirmation);
                    break;
                case "PutThroughDealReply":
                    _invokeMe = new InsertRecord(DataAccessClient.UpdatePutThroughDealReply);
                    break;
                case "DealCancelRequest":
                    _invokeMe = new InsertRecord(DataAccessClient.UpdateDealCancelRequest);
                    break;
                case "DealCancelReply":
                    _invokeMe = new InsertRecord(DataAccessClient.UpdateDealCancelReply);
                    break;
                #endregion

                #region PRS
                case "Securities":
                    if (marketstat.Equals(1))
                        _invokeMe = new InsertRecord(DataAccessClient.UpdateSecuritiesEarly);
                    else
                        _invokeMe = new InsertRecord(DataAccessClient.UpdateSecuritiesLately);

                    //_invokeMe = new InsertRecord(DataAccessClient.UpdateSecurity);
                    break;
                case "MarketStat":
                    _invokeMe = new InsertRecord(DataAccessClient.UpdateMarketStat);
                    break;
                case "TotalMkt":
                    _invokeMe = new InsertRecord(DataAccessClient.UpdateTotalMkt);
                    break;
                case "PutAd":
                    _invokeMe = new InsertRecord(DataAccessClient.UpdatePutAd);
                    break;
                case "PutExec":
                    _invokeMe = new InsertRecord(DataAccessClient.UpdatePutExec);
                    break;
                case "PutDc":
                    _invokeMe = new InsertRecord(DataAccessClient.UpdatePutDc);
                    break;
                case "Le":
                    _invokeMe = new InsertRecord(DataAccessClient.UpdateLe);
                    break;
                case "NewList":
                    _invokeMe = new InsertRecord(DataAccessClient.UpdateNewList);
                    break;
                case "DeList":
                    _invokeMe = new InsertRecord(DataAccessClient.UpdateDeList);
                    break;
                case "Froom":
                    _invokeMe = new InsertRecord(DataAccessClient.UpdateFroom);
                    break;

                #endregion

                default:
                    break;
            }

        }
        public void Execute()
        {
            try
            {
                //Update UI here
                MethodInvoker updateUIState = delegate
                {
                    _lblState.BackColor = Color.Red;
                };
                if (_lblState.InvokeRequired)
                    _lblState.Invoke(updateUIState);
                else
                    updateUIState();
                _invokeMe.BeginInvoke(this.CallBack, null);
            }
            catch (System.Exception e)
            {
                return;
            }
        }
        private void CallBack(IAsyncResult ar)
        {
            try
            {
                int listCount = _invokeMe.EndInvoke(ar);
                //Update UI here
                MethodInvoker updateUIState = delegate
                {
                    _lblState.Text = listCount.ToString();
                    if (ar.IsCompleted)
                        _lblState.BackColor = Color.Green;
                };
                if (_lblState.InvokeRequired)
                    _lblState.Invoke(updateUIState);
                else
                    updateUIState();
            }
            catch (System.Exception e)
            {
                return;
            }
        }
    }
}
